﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using RL.ObjectLayer;
using RL.BusinessLayer;

namespace RL.SMR.Member
{
    public partial class Register : System.Web.UI.Page
    {
        MemberOL _memberOL;
        MemberBL _memberBL;

        protected void Page_Load(object sender, EventArgs e)
        {

        }


        protected void btnAddFC_Click(object sender, EventArgs e)
        {
            _memberOL = new MemberOL();
            DataTable dtFC = new DataTable();

            if (ViewState["FCDetails"] == null)
            {
                dtFC.Columns.Add("Surname", typeof(string));
                dtFC.Columns.Add("Firstname", typeof(string));
                dtFC.Columns.Add("DOB", typeof(string));
                dtFC.Columns.Add("GenderID", typeof(int));
                dtFC.Columns.Add("Gender", typeof(string));
                dtFC.Columns.Add("CitizenshipID", typeof(int));
                dtFC.Columns.Add("Citizenship", typeof(string));
            }
            else
            {
                dtFC = (DataTable)ViewState["FCDetails"];
            }

            dtFC.Rows.Add(txtFCSurname.Text, txtFCFirstName.Text, txtFCDOB.Text, ddlFCGender.SelectedValue, ddlFCGender.SelectedItem, ddlFCCitizenship.SelectedValue, ddlFCCitizenship.SelectedItem);
            ViewState["FCDetails"] = dtFC;

            gwFamilyConn.DataSource = ViewState["FCDetails"];
            gwFamilyConn.DataBind();

            ClearFCFields();
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            SMRSession smrSession = new SMRSession();
            FCMemberOL _fcmemberOL;
            _memberOL = new MemberOL();
            _memberBL = new MemberBL();

            _memberOL.LocalID = txtLocalID.Text;
            _memberOL.FollowersID = txtFollowersID.Text;
            _memberOL.DOC = Convert.ToDateTime(txtDOC.Text);
            _memberOL.DivisionID = ddlDivision.SelectedValue;
            _memberOL.LineageParentID = ddlLineageParent.SelectedIndex;
            _memberOL.LineageNo = txtLineageNo.Text;
            _memberOL.Surname = txtSurname.Text;
            _memberOL.FirstName = txtFirstName.Text;
            _memberOL.DOB = txtDOB.Text;
            _memberOL.GenderID = Convert.ToInt32(ddlGender.SelectedValue);
            _memberOL.CitizenshipID = Convert.ToInt32(ddlCitizenship.SelectedValue);
            _memberOL.PhoneNo = txtPhoneNo.Text;
            _memberOL.CountryID = Convert.ToInt32(ddlCountry.SelectedValue);
            _memberOL.ZIPPostalID = Convert.ToInt32(ddlZIPPO.SelectedValue);
            _memberOL.Address = txtAddress.Text;
            _memberOL.CityTown = txtCityTown.Text;
            _memberOL.Province = txtProvince.Text;
            _memberOL.GuidingParent = txtGuidingParent.Text;
            _memberOL.HOHGuidingParent = txtHOHGuidingParent.Text;
            _memberOL.IDNo = txtIDNo.Text;
            _memberOL.IUserID = _memberOL.UUserID = Convert.ToInt32(smrSession.User.UserID);


            //Preparing to insert Family Connections
            DataTable fcDT = (DataTable)ViewState["FCDetails"];

            List<FCMemberOL> _lstfcmemberOL = new List<FCMemberOL>();

            foreach (DataRow row in fcDT.Rows)
            {
                _fcmemberOL = new FCMemberOL();
                _fcmemberOL.Surname = row[0].ToString();
                _fcmemberOL.FirstName = row[1].ToString();
                _fcmemberOL.DOB = row[2].ToString();
                _fcmemberOL.GenderID = Convert.ToInt32(row[3]);
                _fcmemberOL.CitizenshipID = Convert.ToInt32(row[5]);

                _lstfcmemberOL.Add(_fcmemberOL);
            }

            _memberBL.RegisterMember(_memberOL, _lstfcmemberOL);
            Response.Redirect("View.aspx");
        }

        public void ClearFCFields()
        {
            txtFCSurname.Text = null;
            txtFCFirstName.Text = null;
            txtFCDOB.Text = null;
        }
    }
}